# http://tongji.cnki.net/kns55/index.aspx
import requests
from bs4 import BeautifulSoup
import re
import time
from django.http import HttpResponse
import json

#每次拿去最新的cookies
def updateCookie(cookies,tempCookies):

    for k, v in tempCookies.items():
        cookies[k] = v

    return cookies

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data


def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    trs = soup.select("#ctl00 > table > tbody > tr")[1:]

    for tr in trs:

        tempData = resultData()

        try:
            tempData["titleName"] = tr.select("td")[3].text.strip()
        except:
            pass

        try:
            tempData["source"] = tr.select("td")[-2].text.strip()
        except:
            pass


        allData.append(tempData)



    return allData

def buildingParameters(data):
    # 主题、题名或关键词、题名、第一作者、作者单位、作者、关键词、摘要、基金、DOI
    # 主题:()*作者:()
    typeCorr = {
        "SU": "主题",
        "TI": "题名",
        "AU": "作者",
        "AF": "作者单位",
        "AB": "摘要",
        "KY": "关键词",
    }
    logCorr = {
        "and": "*",
        "or": "+",
        "not": "^",
        "###":"###"
    }

    data["log"].append("###")

    tempStr = []
    for i in range(len(data["values"])):

        try:
            strr = typeCorr[data["type"][i]] + ":" + "(" + data["values"][i] + ")" + "{}".format(logCorr[data["log"][i]])
            tempStr.append(strr)
        except:
            continue

    strr = ''
    for value in tempStr:
        strr += value

    return strr.strip().strip("#").strip()


def getData(keyword,cookies,url):


    headers = {
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://tongji.cnki.net/kns55/brief/result.aspx?stab=shuzhi&t=1&f=1&tt=%e5%8c%97%e4%ba%ac%e6%80%bb%e4%ba%ba%e5%8f%a3%e6%95%b0',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('pagename', 'ASP.brief_result_aspx'),
        ('dbPrefix', 'SCDB'),
        ('dbCatalog', '\u4E2D\u56FD\u5B66\u672F\u6587\u732E\u7F51\u7EDC\u51FA\u7248\u603B\u5E93'),
        ('sTab', ['shuzhi', 'shuzhi']),
        ('ConfigFile', 'SCDB.xml'),
        ('sKuakuID', '1'),
        ('loadgroup', '1'),
    )

    t = time.time()

    rootU = "http://tongji.cnki.net/kns55/brief/brief.aspx?pagename={}&t={}&keyValue={}&S=1".format(url,int(round(t * 1000)),keyword)

    response = requests.get(rootU, headers=headers,cookies=cookies)

    result= formData(response)


    return result

def getCookies(keyword,cookies):

    headers = {
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': '*/*',
        'Referer': 'http://tongji.cnki.net/kns55/brief/result.aspx?stab=shuzhi&t=1&f=1&tt=%e5%8c%97%e4%ba%ac%e6%80%bb%e4%ba%ba%e5%8f%a3%e6%95%b0',
        'Connection': 'keep-alive',
    }

    params = (
        ('action', ''),
        ('PageName', 'ASP.brief_result_aspx'),
        ('DbPrefix', 'SCDB'),
        ('DbCatalog', '\u4E2D\u56FD\u5B66\u672F\u6587\u732E\u7F51\u7EDC\u51FA\u7248\u603B\u5E93'),
        ('ConfigFile', 'SCDB.xml'),
        ('db_value', 'CSYD_TABLEMETA'),
        ('ar_1_sel', 'TM_\u6A21\u7CCA\u5730\u533A'),
        ('ar_1_value1', '\u8BF7\u8F93\u5165\u5730\u533A'),
        ('da_1_sel', '\u5E74\u4EFD>'),
        ('da_1_value1', '\u8D77\u59CB'),
        ('da_2_sel', '\u5E74\u4EFD<'),
        ('da_2_value1', '\u7ED3\u675F'),
        ('stat1_2_sel', 'TM_\u5B8C\u6574\u6307\u6807'),
        ('stat1_2_value1', keyword),
        ('stat1_2_special1', '%'),
        ('src_1_sel', '\u5E74\u9274\u4E2D\u6587\u540D'),
        ('src_1_value1', '\u8BF7\u8F93\u5165\u5E74\u9274\u540D\u79F0'),
        ('his', '1'),
        ('sTab', 'shuzhi'),
        ('__', 'Fri Feb 22 2019 10:59:57 GMT+0800 (\u4E2D\u56FD\u6807\u51C6\u65F6\u95F4)'),
    )
    s = requests.session()

    response = s.get('http://tongji.cnki.net/kns55/request/search.aspx', headers=headers, params=params,cookies=cookies)

    cookies = s.cookies.get_dict()


    return cookies,response.text

def getOneCookies():
    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }
    s = requests.session()

    response = s.get('http://tongji.cnki.net/kns55/index.aspx', headers=headers)

    cookies = s.cookies.get_dict()

    return cookies


def index(request):
    data = json.loads(request.POST.get('data', '1'))

    cookies = getOneCookies()

    cookiesT,url = getCookies(data["values"][0],cookies)

    cookies = updateCookie(cookies,cookiesT)

    data = getData(data["values"][0],cookies,url)

    return HttpResponse(json.dumps({
        "collection": data,
        "allNum": 0
    }), content_type="application/json")